
#include "user_agreement.h"
#include "first_title.h"

#include "page_base.h"
#include "img_common.h"
#include "sys_scene.h"
#include "pt_ipc.h"
#include "pt_info.h"
#include "pt_dev.h"
#include "sys_font.h"
#include "pt_fs.h"

static bool      checkbox_flag = 0;
static lv_obj_t* checkbox_img  = NULL;
static lv_timer_t* timer  = NULL;
static lv_obj_t* btn = NULL;

static lv_obj_t* btn_top1   = NULL;
static lv_obj_t* btn_top2   = NULL;
static lv_obj_t* label_text = NULL;

static  char * icon_checkbox[2] = 
{
    "Check_Default.png",
    "Check_Select.png",    
};

static void user_agree_timer(lv_timer_t * timer)
{
    // if(pt_auth_cancle_get())
    // {
    //     pt_auth_cancle_set(0);
    //     os_printf("rcv auth cancle flag\n");

    //     page_install("link_fail",lv_page_linked_network_fail_create(),NULL);
    //     page_push_anim("link_fail",LOAD_ANIM_MOVE_LEFT);
    // }

}

static int get_checkbox_status(void)
{
    return checkbox_flag;
}

static int checkbox_clicked_event(void)
{
    checkbox_flag = !checkbox_flag;

    midea_ui_set_img_src(checkbox_img , pt_fs_get(icon_checkbox[checkbox_flag]));

    if(get_checkbox_status())
    {
        lv_obj_set_style_bg_color(btn,lv_color_make(0x26,0x7a,0xff),0);
    }else{
        lv_obj_set_style_bg_color(btn,lv_color_make(128,128,128),0);
    }

    return 0;
}


static void checkbox_event_callback(lv_event_t * event)
{
    if(event->code == LV_EVENT_CLICKED)
    {
        debug_d("imgbtn_checkbox clicked\n");
        checkbox_clicked_event();
    }
}

static void use_btn_event_callback(lv_event_t * event)
{
    if(!get_checkbox_status())
        return;

    if(event->code == LV_EVENT_CLICKED)
    {
        debug_d("test clicked\n");

        // pt_user_agreement_set(1);
        // g_scr_save_para.user_agreement = pt_user_agreement_get();
        // pt_ipc_send_msg(IPC_SCR_PARA_SAVE,&g_scr_save_para,sizeof(g_scr_save_para));

        // if(pt_accredit_tokenpwd_get()){
        //     debug_d("get token\n");

        //     page_install("guide1",lv_page_novice_guide1_create(),NULL);
        //     page_push_anim("guide1",LOAD_ANIM_MOVE_LEFT);
        // }else{
        //     debug_d("not get token\n");    
            
        //     page_install("ahthorapp",lv_page_authorization_in_app_create(),NULL);
        //     page_push_anim("ahthorapp",LOAD_ANIM_MOVE_LEFT);
        // }
    }
}

static void use_btn1_event_callback(lv_event_t * event)
{
    if(event->code == LV_EVENT_CLICKED)
    {
        debug_d("btn1\n");
        midea_ui_safe_set_text(label_text, "用户协议内容");

        lv_obj_set_style_border_color(btn_top1,lv_color_make(0x26,0x7a,0xff),LV_PART_MAIN);
        lv_obj_set_style_border_color(btn_top2,lv_color_make(128,128,128),LV_PART_MAIN);
    }
}

static void use_btn2_event_callback(lv_event_t * event)
{
    if(event->code == LV_EVENT_CLICKED)
    {
        debug_d("btn2\n");
        midea_ui_safe_set_text(label_text, "隐私条款内容");

        lv_obj_set_style_border_color(btn_top1,lv_color_make(128,128,128),LV_PART_MAIN);
        lv_obj_set_style_border_color(btn_top2,lv_color_make(0x26,0x7a,0xff),LV_PART_MAIN);

    }
}

void ui_init_user_agreement_page_src(lv_obj_t *superview)
{    
    first_title_set(FIRST_PAGE_USER, 0, 4);

    lv_obj_t * txtbox = lv_obj_create(superview);
    lv_obj_set_style_border_color(txtbox, lv_color_make(0x26,0x26,0x26), LV_PART_MAIN);
    lv_obj_set_style_bg_color(txtbox, lv_color_make(0x26,0x26,0x26), LV_PART_MAIN);
    lv_obj_set_style_bg_opa(txtbox,LV_OPA_100,0);
    lv_obj_set_width( txtbox,422);
    lv_obj_set_height(txtbox,160);
    lv_obj_set_pos(txtbox,18,45);

    label_text = lv_label_create(txtbox);
    lv_obj_set_style_text_font( label_text, lv_font_get(20), LV_STATE_DEFAULT );//字体大小
    lv_obj_set_style_bg_color(label_text, lv_color_make(0x26,0x26,0x26), LV_PART_MAIN);
    lv_obj_set_style_bg_opa(label_text,LV_OPA_100,0);
    lv_obj_set_style_text_color(label_text, lv_color_make(0xff,0xff,0xff), LV_PART_MAIN);
    lv_obj_set_style_text_opa(label_text,LV_OPA_80,0);

    lv_label_set_long_mode(label_text, LV_LABEL_LONG_WRAP);
    lv_obj_set_width(label_text, 385);

    midea_ui_safe_set_text( label_text, "用户协议内容与隐私条款内容：用户协议内容与隐私条款内容：\
    用户协议内容与隐私条款内容：用户协议内容与隐私条款内容：用户协议内容与隐私条款内容：\
    用户协议内容与隐私条款内容：用户协议内容与隐私条款内容：用户协议内容与隐私条款内容：\
    用户协议内容与隐私条款内容：用户协议内容与隐私条款内容：用户协议内容与隐私条款内容：\
    用户协议内容与隐私条款内容：用户协议内容与隐私条款内容：用户协议内容与隐私条款内容：\
    用户协议内容与隐私条款内容：用户协议内容与隐私条款内容：用户协议内容与隐私条款内容：\
    用户协议内容与隐私条款内容：用户协议内容与隐私条款内容：用户协议内容与隐私条款内容：");

    lv_obj_t * checkbox_obj = lv_obj_create(superview);
    midea_ui_safe_remove_style_all(checkbox_obj);//清除所有样式
    lv_obj_set_pos(checkbox_obj,160,212);
    midea_ui_safe_set_size(checkbox_obj,32,32);

    lv_obj_clear_flag(checkbox_obj,LV_OBJ_FLAG_GESTURE_BUBBLE);
    lv_obj_add_event_cb(checkbox_obj, checkbox_event_callback, LV_EVENT_ALL, NULL);

    checkbox_img = lv_img_create(checkbox_obj);
    midea_ui_set_img_src(checkbox_img, pt_fs_get(icon_checkbox[checkbox_flag]));

    lv_obj_t * label_checkbox = lv_label_create(superview);
    lv_obj_set_style_text_font(label_checkbox, lv_font_get(20), LV_STATE_DEFAULT );
    lv_obj_set_style_text_color(label_checkbox,lv_color_make(0xff,0xff,0xff),LV_PART_MAIN);
    lv_obj_set_style_text_opa(label_checkbox,LV_OPA_80,0);
    midea_ui_safe_set_text( label_checkbox, "已阅读并同意");
    midea_ui_safe_obj_align(label_checkbox,LV_ALIGN_TOP_LEFT,195,214);

    btn = lv_btn_create(superview);
    lv_obj_set_style_radius(btn, LV_PCT(25), LV_PART_MAIN);
    lv_obj_set_style_outline_width(btn, 0, LV_PART_MAIN);
	lv_obj_set_style_shadow_width(btn, 0, LV_PART_MAIN);

    lv_obj_set_style_bg_opa(btn,LV_OPA_100,0);
    lv_obj_set_pos(btn,65,250);
    midea_ui_safe_set_size(btn,350,50);

    if(get_checkbox_status())
    {
        lv_obj_set_style_bg_color(btn,lv_color_make(0x26,0x7a,0xff),0);
    }else{
        lv_obj_set_style_bg_color(btn,lv_color_make(128,128,128),0);
    }

    lv_obj_t * btn_label = lv_label_create(btn);
    lv_obj_set_style_text_font( btn_label, lv_font_get(24), LV_STATE_DEFAULT );//字体大小
    midea_ui_safe_set_text(btn_label,"开始使用");
    lv_obj_center(btn_label);

    lv_obj_clear_flag(btn,LV_OBJ_FLAG_GESTURE_BUBBLE);
    lv_obj_add_event_cb(btn, use_btn_event_callback, LV_EVENT_ALL, NULL);
    //***********************************************************

    btn_top1 = lv_obj_create(superview);

    lv_obj_set_pos(btn_top1,0,0);
    lv_obj_set_size(btn_top1,100,30);

    lv_obj_set_style_radius(btn_top1,15, LV_PART_MAIN);
	lv_obj_set_style_border_width(btn_top1,2, LV_PART_MAIN);
    lv_obj_set_style_border_opa(btn_top1, LV_OPA_100, LV_PART_MAIN);
	lv_obj_set_style_border_color(btn_top1, lv_color_make(0x26,0x7a,0xff), LV_PART_MAIN);
	lv_obj_set_scrollbar_mode(btn_top1, LV_SCROLLBAR_MODE_OFF);
	lv_obj_set_style_pad_all(btn_top1, 0,0);

    #if 1
	lv_obj_set_style_bg_opa(btn_top1, LV_OPA_0, LV_PART_MAIN);
    #else
	lv_obj_set_style_bg_opa(btn_top1, LV_OPA_100, LV_PART_MAIN);
	lv_obj_set_style_bg_color(btn_top1, lv_color_make(0xFF,0,0), LV_PART_MAIN);
    #endif

    lv_obj_add_event_cb(btn_top1, use_btn1_event_callback,LV_EVENT_ALL,NULL);
    //***********************************************************
    btn_top2 = lv_obj_create(superview);

    lv_obj_set_pos(btn_top2,110,0);
    lv_obj_set_size(btn_top2,100,30);

    lv_obj_set_style_radius(btn_top2,15, LV_PART_MAIN);
	lv_obj_set_style_border_width(btn_top2,2, LV_PART_MAIN);
    lv_obj_set_style_border_opa(btn_top2, LV_OPA_100, LV_PART_MAIN);
	lv_obj_set_style_border_color(btn_top2, lv_color_make(128,128,128), LV_PART_MAIN);
	lv_obj_set_scrollbar_mode(btn_top2, LV_SCROLLBAR_MODE_OFF);
	lv_obj_set_style_pad_all(btn_top2, 0,0);

    #if 1
	lv_obj_set_style_bg_opa(btn_top2, LV_OPA_0, LV_PART_MAIN);
    #else
	lv_obj_set_style_bg_opa(btn_top2, LV_OPA_100, LV_PART_MAIN);
	lv_obj_set_style_bg_color(btn_top2, lv_color_make(0,0xff,0), LV_PART_MAIN);
    #endif

    lv_obj_add_event_cb(btn_top2,use_btn2_event_callback,LV_EVENT_ALL,NULL);
    //***********************************************************
    timer = lv_timer_create(user_agree_timer, 1000,  NULL);
    //***********************************************************
}

static void user_agreement_page_on_custom_attr_config(page_base_t* self)
{
    return ;
}
static void user_agreement_page_on_view_load(page_base_t* self)
{
    ui_init_user_agreement_page_src(self->root);
    return ;
}
static void user_agreement_page_on_view_did_load(page_base_t* self)
{
    return ;
}
static void user_agreement_page_on_view_will_appear(page_base_t* self)
{
    return ;
}
static void user_agreement_page_on_view_did_appear(page_base_t* self)
{
    return ;
}
static void user_agreement_page_on_view_will_disappear(page_base_t* self)
{
    return ;
}
static void user_agreement_page_on_view_did_disappear(page_base_t* self)
{
    return ;
}
static void user_agreement_page_on_view_did_unload(page_base_t* self)
{
    if(timer){
        midea_ui_safe_del_timer(timer);
        timer = NULL;
    }
    if(btn_top1){
        midea_ui_safe_del_obj(btn_top1);
        btn_top1 = NULL;
    }
    if(btn_top2){
        midea_ui_safe_del_obj(btn_top2);
        btn_top2 = NULL;
    }
    if(label_text){
        midea_ui_safe_del_obj(label_text);
        label_text = NULL;
    }


    return ;
}

static page_vtable_t page_param = {
    .on_custom_attr_config  = user_agreement_page_on_custom_attr_config,
    .on_view_load           = user_agreement_page_on_view_load,
    .on_view_did_load       = user_agreement_page_on_view_did_load,
    .on_view_will_appear    = user_agreement_page_on_view_will_appear,
    .on_view_did_appear     = user_agreement_page_on_view_did_appear,
    .on_view_will_disappear = user_agreement_page_on_view_will_disappear,
    .on_view_did_disappear  = user_agreement_page_on_view_did_disappear,
    .on_view_did_unload     = user_agreement_page_on_view_did_unload,
};


page_vtable_t* lv_page_user_agreement_create(void)
{
    return &page_param;
}
